<template>
	<view class="comment-container">
		<view class="goods-info">
			<view class="goods-left">
				<view class="goods-name">
					 {{ goodsInfo.goodsName }}
				</view>
			</view>
			<view class="goods-right">
				<view class="goods-pic">
					<image v-if="goodsInfo.goodsImg" :src="$imageUrl + goodsInfo.goodsImg"></image>
				</view>
			</view>
		</view>
		<view class="comment-wrapper">
			<view class="comment-box">
				<view class="comment-count">
					好物评价（{{ total }}）
				</view>
				<view class="comment-add" @click="comment">
					<text>评价</text>+
				</view>
			</view>
			<view class="comment-list">
				<view class="comment-item" v-for="comment in commentList" :key="comment.id">
					<view class="userinfo">
						<view class="userinfo-left">
							<view class="avatar">
								<image :src="$imageUrl + comment.avatar" mode=""></image>
							</view>
							<text class="username">{{ comment.nickName }}</text>
						</view>
						<view class="userinfo-right">
							<view class="publish-time">
								{{ comment.publishTime }}
							</view>
						</view>
					</view>
					<view class="comment-content">
						{{ comment.content }}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsInfo: {},
				queryParams: {
					status: 1,
					page: 1,
					pageSize: 6,
				},
				commentList: [],
				total: 0,
				loading: false,
			};
		},
		onLoad(e) {
			this.getData(e.goodsId)
		},
		methods: {
			async getData(goodsId) {
				const {
					data: res
				} = await uni.$http.get(`/goods/${goodsId}`)
				this.goodsInfo = res.data
				this.queryParams.goodsId = goodsId
				const { data: comments } = await uni.$http.get('/comments/page', this.queryParams)
				this.commentList = comments.data.records
				this.total = comments.data.total
			},
			comment() {
				console.log('添加評論');
				uni.navigateTo({
					url: `/subcontract/add_comment/add_comment?goodsId=${this.goodsInfo.goodsId}`
				})
			},
			async getCommentList(callback) {
				this.loading = true
				const { data: comments } = await uni.$http.get('/comments/page', this.queryParams)
				this.loading = false
				callback && callback()
				this.commentList = [...this.commentList, ...comments.data.records]
				this.total = comments.data.total
			},
			onPullDownRefresh() {
				this.queryParams.page = 1
				this.commentList = []
				this.loading = false
				this.getCommentList(() => {
					uni.stopPullDownRefresh()
				})
			},
			onReachBottom() {
				if(this.queryParams.page * this.queryParams.pageSize >= this.total) return uni.$showMsg('数据加载完毕 T_T')
				if(this.loading) return
				this.queryParams.page++
				this.getCommentList()
			},
		},
	}
</script>

<style lang="scss">
.comment-container {
	background-color: #f9f9f9;
	height: 100%;
	width: 100%;
	display: flex;
	flex-direction: column;
}
.goods-info {
	padding: 10px;
	box-sizing: border-box;
	width: 100%;
	background-color: #fff;
	margin-bottom: 10px;
	display: flex;
	.goods-left {
		flex-grow: 1;
	}
	.goods-right {
		margin-left: 10px;
		.goods-pic {
			float: right;
			width: 80px;
			height: 80px;
		}
		image {
			width: 100%;
			height: 100%;
		}
	}
}
.comment-wrapper {
	flex-grow: 1;
	padding: 10px;
	background-color: #fff;
}
.comment-box {
	display: flex;
	justify-content: space-between;
	box-sizing: border-box;
	color: #000;
	font-weight: bold;
	height: 30px;
	line-height: 60rpx;
	.comment-count {
		vertical-align: middle;
	}
	.comment-add {
		color: #d9d9d9;
		font-size: 20px;
		font-weight: normal;
		display: flex;
		align-items: center;
		vertical-align: middle;
		text {
			font-size: 12px;
		}
	}
}

.comment-list {
	.see-all {
		color: #d9d9d9;
		font-size: 12px;
		text-align: center;
		letter-spacing: 4rpx;
	}
}

.comment-item {
	margin: 20rpx 0;
	padding-bottom: 10px;
	border-bottom: 1px #d9d9d9 solid;
	.userinfo {
		display: flex;
		margin-bottom: 20rpx;
		justify-content: space-between;
		.userinfo-left {
			display: flex;
		}
		.avatar {
			width: 50rpx;
			height: 50rpx;
			overflow: hidden;
			border-radius: 50%;
			margin-right: 10rpx;
			image {
				width: 100%;
				height: 100%;
			}
		}
		.username {
			display: flex;
			font-size: 14px;
			align-items: center;
		}
		.publish-time {
			font-size: 12px;
		}
	}
}

</style>
